let jwt = require('jsonwebtoken');

module.exports = () => {
    return async (ctx,next) => {
        console.log(ctx.request.method)
        let writeArr = ['/api/login','/api/registry','/api/code'];

        if(writeArr.includes(ctx.request.url)){
            await next();
        }else{
            let token = ctx.get('token');

            // try{
                let obj = jwt.verify(token,'lixd'); //{username,user_id}
                console.log(obj)
                ctx.request.user_id = obj.user_id;
                await next();
            // }catch(e){
            //     if(e.name === 'TokenExpiredError'  || e.name === 'JsonWebTokenError'){
            //         ctx.status = 401;
            //         ctx.body = {
            //             code:6,
            //             msg:'鉴权失败'
            //         }
            //     }else{
            //         ctx.body = {
            //             code:5,
            //             msg:e
            //         }
            //     }
            // }
        }
    }
}